package com.wei.czz.common.aspect;

import com.wei.czz.common.constant.Constant;
import com.wei.czz.framework.common.service.SnowflakeService;
import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-06-03 21:26:41
 * className: JobAspect 定时任务切片类
 * version: 1.0
 * description:
 */
@Aspect
@Component
@AllArgsConstructor
public class JobAspect {

    private static final Logger logger = LoggerFactory.getLogger(JobAspect.class);

    private final SnowflakeService snowflakeService;

    /**
     * com.wei.blog.framework.job包路径下，所有的方法作为切点
     */
    @Pointcut("execution(* com.wei.czz.framework.job.*.*(..))")
    public void logPoint() {}

    /**
     * Aop环绕通知方法
     */
    @Around("logPoint()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        // 增加日志链路追踪代码
        String traceId = Long.toHexString(snowflakeService.get());
        MDC.put(Constant.TRACE_ID, traceId);
        MDC.put(Constant.SPAN_ID, traceId);

        try {
            // 执行目标方法
            return pjp.proceed();
        } finally {
            MDC.clear();
        }
    }

}
